<template>
	<div class="system-dic-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="450px">
			<el-form ref="dicDialogFormRef" :model="state.ruleForm" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="任务标题">
							<el-input v-model="state.ruleForm.title" placeholder="请输入任务标题" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="任务类型">
							<el-select v-model="state.ruleForm.type" placeholder="请选择任务类型" clearable size="default" style="width: 100%;">
								<el-option v-for="(item,index) in state.types" :key="'type'+index" :label="item.name" :value="item.value"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="执行表达式">
							<el-input v-model="state.ruleForm.rule" placeholder="请输入执行表达式" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="任务字符串">
							<el-input v-model="state.ruleForm.target" placeholder="请输入任务字符串" clearable></el-input>
						</el-form-item>
					</el-col>
					<!-- <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="备注">
							<el-input v-model="state.ruleForm.remark" placeholder="请输入备注" clearable></el-input>
						</el-form-item>
					</el-col> -->
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="排序">
							<el-input-number :max="99" :min="0" v-model="state.ruleForm.sort" controls-position="right" placeholder="请输入排序" class="w100" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="状态">
							<el-switch v-model="state.ruleForm.status" :active-value="1" :inactive-value="0" inline-prompt active-text="启用" inactive-text="禁用"></el-switch>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="单次执行">
							<el-switch v-model="state.ruleForm.singleton" :active-value="0" :inactive-value="1" inline-prompt active-text="是" inactive-text="否"></el-switch>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="systemDicDialog">
import { reactive, ref } from 'vue';
import { systemCrontabApi } from '/@/api/system/crontab/index';
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const dicDialogFormRef = ref();
const state = reactive({
	types:[{
			value:1,
			name:'command'
		},{
			value:2,
			name:'class'
		},{
			value:3,
			name:'url'
		},{
			value:4,
			name:'shell'
		}
	],
	ruleForm: {
		title: '',
		type: '',
		rule: '',
		target: '',
		parameter: '',
		// remark: '',
		sort: 0,
		status: 1,
		singleton:0
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = (type, row) => {
	state.dialog.type = type;
	if (type === 'edit') {
		state.ruleForm = JSON.parse(JSON.stringify(row));
		console.log(state.ruleForm);
		// state.ruleForm.type = parseInt(row.type);
		state.dialog.title = '修改定时器';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增定时器';
		state.dialog.submitTxt = '新 增';
		state.ruleForm = {
			title: '',
			type: '',
			rule: '',
			target: '',
			parameter: '',
			remark: '',
			sort: 0,
			status: 1,
			singleton:0
		}
	}
	state.dialog.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	if (state.dialog.type === 'add') { 
		systemCrontabApi().add(state.ruleForm).then(async res=>{
			closeDialog(); // 关闭弹窗
			emit('refresh');
		})
	}else{
		systemCrontabApi().edit(state.ruleForm).then(async res=>{
			closeDialog(); // 关闭弹窗
			emit('refresh');
		})
	}
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
